package mn.slide.laborberlin.main;

import java.util.ArrayList;
import java.util.List;

import mn.slide.laborberlin.model.PListInnerModel;
import mn.slide.laborberlin.model.PListMainModel;
import mn.slide.laborberlin.tabgroup.GroupStarter;
import mn.slide.laborberlin.tabgroup.HomeGroupActivity;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.OverlayItem;

public class StandardLaborActivity extends MapActivity
{

	private MapView						mapView;
	private MapController				mapController;
	private MyLocationOverlay			me							= null;
	private final double				kCenterPointLatitude		= 52.525924;
	private final double				kCenterPointLongitude		= 13.376884;
	private final double				kSpanDeltaLatitude			= 0.09263112;
	private final double				kSpanDeltaLongitude			= 0.01697419;
	private final double				iphoneScaleFactorLatitude	= 9.0;
	private final double				iphoneScaleFactorLongitude	= 11.0;
	private List<OverlayItem>			items;
	private ArrayList<PListMainModel>	mainList;
	private List<PListInnerModel>		childList;

	EditText							search_edt;

	@Override
	protected void onCreate( Bundle savedInstanceState )
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.standard);

		items = new ArrayList<OverlayItem>();
		childList = new ArrayList<PListInnerModel>();
		mainList = getIntent().getParcelableArrayListExtra("mainList");
		for (PListMainModel inner : mainList)
		{
			for (PListInnerModel child : inner.getChild())
			{
				Log.v("StandardLaborActivity", child.getChild().getLatitude() + "  " + child.getChild().getLongtitude());
				childList.add(child);
				items.add(new OverlayItem(getPoint(Double.parseDouble(child.getChild().getLatitude()), Double.parseDouble(child.getChild().getLongtitude())), child.getChild().getTitle(), child.getChild().getName2()));
			}
		}
		search_edt = (EditText) findViewById(R.id.search_edt);
		search_edt.setOnKeyListener(new OnKeyListener()
		{
			public boolean onKey( View v , int keyCode , KeyEvent event )
			{
				// If the event is a key-down event on the "enter" button
				if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER))
				{
					String k = search_edt.getText().toString();
					if (k.length() > 0)
					{
						EditText searchbox = (EditText) v;
						InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
						imm.hideSoftInputFromWindow(searchbox.getWindowToken(), 0);
						Intent intent = new Intent(StandardLaborActivity.this, SearchActivity.class);
						intent.putExtra("searchTxt", k);
						HomeGroupActivity.group.goTo(intent);

					}
					return true;
				}
				return false;
			}
		});
		mapView = (MapView) findViewById(R.id.map);
		mapController = mapView.getController();
		mapController.setCenter(getPoint(kCenterPointLatitude, kCenterPointLongitude));
		Drawable marker = getResources().getDrawable(R.drawable.map_marker);

		marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker.getIntrinsicHeight());
		mapView.getOverlays().add(new ItemOverlay(marker, this, this.items)
		{
			@Override
			public boolean onTouchEvent( MotionEvent event , MapView mapView )
			{
				// TODO Auto-generated method stub
				return super.onTouchEvent(event, mapView);
			}
		});
		mapController.setZoom(12);

		mapView.setBuiltInZoomControls(true);
		me = new MyLocationOverlay(this, mapView);
		mapView.getOverlays().add(me);
		Log.v("StandardLaborActivity", "FINISH");

	}

	private GeoPoint getPoint( double lat , double lon )
	{
		return (new GeoPoint((int) (lat * 1000000.0), (int) (lon * 1000000.0)));
	}

	@Override
	public void onResume()
	{
		super.onResume();
	}

	private class ItemOverlay extends ItemizedOverlay<OverlayItem>
	{
		private Activity	context;
		private Drawable	marker;
		List<OverlayItem>	items			= new ArrayList<OverlayItem>();
		private View		currentPopup	= null;

		public ItemOverlay( Drawable defaultMarker , Activity context , List<OverlayItem> items )
		{
			super(boundCenterBottom(defaultMarker));
			Animation anim = AnimationUtils.loadAnimation(context, R.anim.in_from_top);
			anim.start();
			this.items = items;
			this.marker = defaultMarker;
			this.context = context;
			populate();
		}

		public void addOverlay( OverlayItem overlay )
		{
			items.add(overlay);
			populate();
			mapView.invalidate();
		}

		@Override
		protected OverlayItem createItem( int i )
		{
			return items.get(i);
		}

		@Override
		protected boolean onTap( final int i )
		{

			mapView.getController().animateTo(items.get(i).getPoint());

			if (currentPopup != null)
			{
				mapView.removeView(currentPopup);
				currentPopup = null;
			}
			MapView.LayoutParams mapParams = new MapView.LayoutParams(MapView.LayoutParams.WRAP_CONTENT, MapView.LayoutParams.WRAP_CONTENT, items.get(i).getPoint(), 0, // x-offset
			-30, MapView.LayoutParams.BOTTOM_CENTER);
			View popup = getLayoutInflater().inflate(R.layout.map_popup, mapView, false);
			mapView.addView(popup, mapParams);
			TextView title = (TextView) popup.findViewById(R.id.title_popup);
			title.setText(items.get(i).getTitle());
			TextView desc = (TextView) popup.findViewById(R.id.desc_popup);
			desc.setText(items.get(i).getSnippet());
			popup.startAnimation(AnimationUtils.loadAnimation(StandardLaborActivity.this, R.anim.fadeup_in));
			Button stan_btn = (Button) popup.findViewById(R.id.stan_btn);
			stan_btn.setOnClickListener(new View.OnClickListener()
			{

				@Override
				public void onClick( View v )
				{
					Intent intent = new Intent(context, AnsprechparterDetialActivity.class);
					intent.putExtra("model", childList.get(i).getChild());
					GroupStarter.getInstance().startActivity(context, intent);
				}
			});
			currentPopup = popup;

			return (true);
		}

		@Override
		public int size()
		{
			return items.size();
		}

	}

	@Override
	protected boolean isRouteDisplayed()
	{
		return (false);
	}

	@Override
	public void onBackPressed()
	{

		GroupStarter.getInstance().onBackpressed(this);
	}

}
